package com.hzvdvmc.autosale.common;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.hzvdvmc.autosale.MyApp;
import com.hzvdvmc.autosale.constant.TimeConstant;
import com.hzvdvmc.autosale.sale.MainActivity;
import com.hzvdvmc.autosale.util.LogToFileHelper;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.ToastUtils;

/**
 * Created by william on 2018/1/25.
 */

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    protected boolean isNeedCaughtExeption = true;// 是否捕获未知异常
    private static CrashHandler instance;
    private Application appContext;

    public static CrashHandler getInstance() {
        if (instance == null) {
            instance = new CrashHandler();
        }
        return instance;
    }

    public void init(Application context) {
        this.appContext = context;
        if (isNeedCaughtExeption)
            Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        // 以下用来捕获程序崩溃异常
        LogUtil.e(e.getMessage());
        LogUtil.e(e.getStackTrace().toString());
        restartApp();
        ToastUtils.showShort("程序异常已重启");
        LogToFileHelper.e("hahaha", e.getMessage());
    }

    private void restartApp() {
        //退出程序
        ((MyApp) (appContext)).restartApp();
    }

}
